package lab.partitioner

/**
  * Created by kawhi on 30/03/2017.
  */


import org.apache.spark.{HashPartitioner, SparkConf, SparkContext}

object Hash {

  def showPartition[T](i: Int, iter: Iterator[T]) : Iterator[(T)] = {
    iter.foreach(ele => {
      println("partition\t" + i + "\t" + ele)
    })
    iter
  }
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("Hash Partitioner").setMaster("local[4]")
    val sc = new SparkContext(conf)
    val ports = sc.parallelize(List('a','b','c', 'a', 'a', 'a')).map((_, 1))
    val wbt = ports.partitionBy(new HashPartitioner(3))

    val res = wbt.mapPartitionsWithIndex(showPartition).collect

    sc.stop()
  }
}
